package com.utils.lib.utilsview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.utils.lib.R;


public class UDialog extends Dialog {
    public UDialog(@NonNull Context context) {
        super(context);
        init();
    }

    public UDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    public UDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private Context context;
    TextView title, left, right, msg;
    private String mTitle;
    private String confirmButtonText;
    private String cacelButtonText;
    private String MSG;
    private ClickListener l;

    public UDialog(@NonNull Context context, Context context1, ClickListener listener) {
        super(context);
        init();
        this.context = context1;
        this.l = listener;
    }

    public String getmTitle() {
        return mTitle;
    }

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
        title.setText(mTitle);
    }

    public String getConfirmButtonText() {
        return confirmButtonText;
    }

    public void setConfirmButtonText(String confirmButtonText) {
        this.confirmButtonText = confirmButtonText;
        right.setText(confirmButtonText);
    }

    public String getCacelButtonText() {
        return cacelButtonText;
    }

    public void setCacelButtonText(String cacelButtonText) {
        this.cacelButtonText = cacelButtonText;
        left.setText(cacelButtonText);
    }

    public interface ClickListener {

        public void doConfirm();

        public void doCancel();

        public void doDismiss();
    }


    public UDialog(Context context, String title, String confirmButtonText, String cacelButtonText, ClickListener listener) {
        super(context, R.style.u_dialog);
        init();
        this.context = context;
        this.mTitle = title;
        this.confirmButtonText = confirmButtonText;
        this.cacelButtonText = cacelButtonText;
        this.l = listener;
    }

    public UDialog(Context context, String title, String confirmButtonText, String cacelButtonText, String msg, ClickListener listener) {
        super(context, R.style.u_dialog);
        init();
        this.context = context;
        this.mTitle = title;
        this.confirmButtonText = confirmButtonText;
        this.cacelButtonText = cacelButtonText;
        this.l = listener;
        this.MSG = msg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.exit_dialog_layout1, null);
        setContentView(view);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.7); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
        title = findViewById(R.id.u_tv_title1);
        left = findViewById(R.id.u_tv_left1);
        right = findViewById(R.id.u_tv_right1);
        msg = findViewById(R.id.u_tv_msg1);
        TextView line = findViewById(R.id.tv_line);
        if (null != mTitle) title.setText(mTitle);
        if (null != cacelButtonText) left.setText(cacelButtonText);
        if (null != confirmButtonText) right.setText(confirmButtonText);
        if (null != MSG) msg.setText(MSG);
        msg.setVisibility(TextUtils.isEmpty(MSG) ? View.GONE : View.VISIBLE);
        line.setVisibility(TextUtils.isEmpty(MSG) ? View.GONE : View.VISIBLE);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isOnclick = false;
                dismiss();
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isOnclick = false;
                if (null != l) l.doConfirm();
            }
        });
    }

    private boolean isOnclick = true;

    @Override
    public void show() {
        isOnclick = true;
        super.show();
    }

    @Override
    public void dismiss() {
        if (isOnclick) {
            if (null != l) l.doDismiss();
        }
        super.dismiss();
    }

    public void init() {

    }
}